package com.itheima.blog.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.itheima.blog.admin.domain.po.OperationLogTable;
import com.itheima.blog.admin.dto.LogReqDto;
import com.itheima.blog.admin.service.IOperationLogTableService;
import com.itheima.common.domain.PageResDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/system")
@RestController
@RequiredArgsConstructor
@Api("系统相关接口")
public class SystemController {
    private final IOperationLogTableService logService;

    @PostMapping("/operate/log")
    @ApiOperation("操作日志分页查询")
    public PageResDto<OperationLogTable> page(LogReqDto dto){
        Page<OperationLogTable> page = logService.lambdaQuery()
                .like(StringUtils.isNotBlank(dto.getUserName()), OperationLogTable::getUserName, dto.getUserName())
                .like(StringUtils.isNotBlank(dto.getInterfaceName()), OperationLogTable::getInterfaceName, dto.getInterfaceName())
                .like(StringUtils.isNotBlank(dto.getIp()), OperationLogTable::getIp, dto.getIp())
                .lt(dto.getTimeConsuming() != null, OperationLogTable::getTimeConsuming, dto.getTimeConsuming())
                .gt(dto.getBeginTime() != null, OperationLogTable::getCreationTime, dto.getBeginTime())
                .lt(dto.getEndTime() != null, OperationLogTable::getCreationTime, dto.getEndTime())
                .page(PageDTO.of(dto.getPage(), dto.getSize()));
        return new PageResDto<>(dto,page.getTotal(),page.getRecords());
    }
}
